package m2.component;
import java.util.ArrayList;

import m2.configuration.Config;
import m2.port.Port;
import m2.property.Property;
import m2.service.Service;


public abstract class Component {

        private ArrayList<Property> listeProperty;
        private ArrayList<ComponentInterface> listeComponentInterface;
        private ArrayList<Component> listeComponent;
        private String owner;
        private Config config;
        private ComponentInterface componentInterfaceProvided;
        private ComponentInterface componentInterfaceRequired;
       
        /**
         * 
         */
        public Component(){
        	
                listeProperty = new ArrayList<Property>();
                listeComponentInterface = new ArrayList<ComponentInterface>() ;        
                listeComponent = new ArrayList<Component>();
        }
       
        /**
         * 
         * @param componentInterfaceRequired
         */
        public final void addComponentInterfaceRequired(ComponentInterface componentInterfaceRequired){
                this.componentInterfaceRequired = componentInterfaceRequired;
                listeComponentInterface.add(componentInterfaceRequired);
        }
       
        /**
         * 
         * @param componentInterfaceProvided
         */
        public final void addComponentInterfaceProvided(ComponentInterface componentInterfaceProvided){
                this.componentInterfaceProvided = componentInterfaceProvided;
                listeComponentInterface.add(componentInterfaceProvided);
        }
       
        /**
         * 
         * @param properties
         */
        public final void addProperty(Property ... properties){
                for (Property property : properties){
                        listeProperty.add(property);
                }
        }
       
        /**
         * 
         * @param components
         */
        public final void addComponent(Component ... components){
                for (Component component : components){
                        component.setOwner(this.getClass().getSimpleName());
                        listeComponent.add(component);
                }
        }
       
        /**
         * 
         * @param config
         */
        public final void addConfig(Config config){
                config.setOwner(this.getClass().getSimpleName());
                this.config = config;
        }
       
        /**
         * 
         * @param requiredPorts
         */
        public final void addRequiredPort(Port ... requiredPorts){      
                for (Port port : requiredPorts){
                        port.setOwner(this.getClass().getSimpleName());
                        port.setType("required");
                        componentInterfaceRequired.setPort(port);
                }              
        }
       
        /**
         * 
         * @param providedPorts
         */
        public final void addProvidedPort(Port ... providedPorts){      
                for (Port port : providedPorts){
                        port.setOwner(this.getClass().getSimpleName());
                        port.setType("provided");
                        componentInterfaceProvided.setPort(port);
                }              
        }      
        
        /**
         * 
         * @return
         */
        public ArrayList<Component> getListeComponent() {
                return listeComponent;
        }

        /**
         * 
         * @param listeComponent
         */
        public void setListeComponent(ArrayList<Component> listeComponent) {
                this.listeComponent = listeComponent;
        }
       
        /**
         * 
         * @param name
         * @return
         */
        public Component getComponent(String name){
                for (Component component : listeComponent){
                        if (component.getClass().getSimpleName().equals(name)){
                                return component;
                        }
                }
                return null;
        }
       
        /**
         * 
         * @param name
         * @return
         */
        public Property getProperty(String name){
                for (Property property : listeProperty){
                        if (property.getClass().getSimpleName().equals(name)){
                                return property;
                        }
                }
                return null;
        }
       
        /**
         * 
         * @param name
         * @return
         */
        public Port getRequiredPort(String name){
                return componentInterfaceRequired.getPort(name);
        }
       
        /**
         * 
         * @param name
         * @return
         */
        public Port getProvidedPort(String name){
                return componentInterfaceProvided.getPort(name);
        }
       
        /**
         * 
         * @return
         */
        public Config getConfig(){
                return config;
        }
       
        /**
         * 
         * @return
         */
        public String getOwner() {
                return owner;
        }

        /**
         * 
         * @param owner
         */
        public void setOwner(String owner) {
                this.owner = owner;
        }
         
        /**
         * 
         * @param serviceName
         * @param message
         */
        public void startService(String serviceName, String message){
        	for (ComponentInterface  componentInterface : listeComponentInterface){
        		ArrayList<Service> services = componentInterface.getListeService();
        		for(Service service : services){
        			if (service.getNameService().equals(serviceName)){
        				service.activate(message);
        			}
        		}
        	}
        }        
}
